package com.ruoyi.web.controller.app.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.exception.base.XException;
import com.ruoyi.sj.domain.Comments;
import com.ruoyi.sj.mapper.CommentsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MyCommentService
 * @Description TODO
 * @Author tyc@yanyu
 * @Date 2018/12/7 0007 11:20
 **/
@Service
public class AppCommentService {
    @Autowired
    private CommentsMapper commentsMapper;

    /**
     * 发布评论
     *
     * @return
     */
    public Object addComment(String content, Long dynamicId, Long toMemberId, Long fromMemberId) {
        Comments comments = new Comments();
        comments.setContent(content);
        if (dynamicId == null) throw new XException("动态id不能为空");
        comments.setDynamicId(dynamicId); //动态id
        if (fromMemberId == null) throw new XException("评论人ID号不能为空");
        comments.setFromMemberId(fromMemberId); //评论人ID号
        if (toMemberId != null) comments.setToMemberId(toMemberId);//被评论用户ID号
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        comments.setCreateDate(df.format(new Date()));  //发布日期
        return commentsMapper.insertComments(comments);
    }

    /**
     * 评论列表
     *
     * @param dynamicId
     * @return
     */
    public Object queryCommentList(String dynamicId) {
        if (StringUtils.isBlank(dynamicId)) {
            throw new XException("动态id不能为空");
        }
        Comments comments = new Comments();
        comments.setDynamicId(Long.valueOf(dynamicId));
        List<Comments> list = commentsMapper.selectCommentsList(comments);
        return list;
    }

    /**
     * 删除动态下的某一条评论
     *
     * @param id
     * @param dynamicId
     * @return
     */
    public Object deleteComments(String id, String dynamicId) {
        if (StringUtils.isBlank(id)) {
            throw new XException("id不能为空");
        }
        if (StringUtils.isBlank(dynamicId)) {
            throw new XException("动态id不能为空");
        }
        return commentsMapper.deleteComment(id, dynamicId);
    }

    public Long countNum(String dynamicId) {
        return commentsMapper.countNum(dynamicId);
    }
}
